import initConfig from '@/mixins/shop/settlement/settlement.js'
import data from '@/data/shop/settlement/settlement.js'
import BigNumber  from "@/common/js/bignumber.js"

export default {
	mixins:[initConfig],
	data(){
		return {
			defaultAddress:[],
			cartGoodsList:[],
			trigger:"",
			direct_settlement_param:{
				id:0,
				quantity:0,
				spec_key:"",
				spec_key_name:""
			}
		}
	},
	async onLoad(param){
		const defaultAddress = data.defaultAddress();
		this.defaultAddress = await defaultAddress;
		
		const cartGoodsList = data.cartGoodsList();
		this.cartGoodsList = await cartGoodsList;
		
		this.trigger = param.trigger || "settlement";
		
		//直接购买过来的
		if(param.id && param.quantity && param.spec_key && param.spec_key_name){
				
			this.direct_settlement_param = this.$Helper.depthClone(param);
			
			param.type="buy";
			
			this.trigger = "direct_settlement";
			
			this.$Helper.setCache("direct_buy_data",param,1200); 		//二十分钟缓存
			
		}
		
		this.$Helper.getCache("direct_buy_data") && this.trigger=="direct_settlement" && Object.assign(param,this.$Helper.getCache("direct_buy_data"),param);
	},
	methods:{
		changeNumber(e){
			let goods_id = 0;
			this.cartGoodsList.forEach((seller,sellerIndex)=>{
				seller.id == e.seller_id && seller.goods.forEach((goods,goodsIndex)=>{
					if(goods.id == e.cart_id){
						this.cartGoodsList[sellerIndex].goods[goodsIndex].number = e.number;
						this.direct_settlement_param.quantity=e.number;
						goods_id = this.cartGoodsList[sellerIndex].goods[goodsIndex].goods_id;
					}
				})
			});
			
			// 直接购买，就不用去改变购买数量,如果是通过购物车结算的方式，就去变更购物车的数量
			if(this.trigger == "settlement" && e.trigger == "artificial"){
				let param={
					type:"settlement_stock",
					settlement_way:"cart",
					quantity:e.number,
					goods_id:goods_id,
					id:e.cart_id
				};
			}

		},
		navBarIconTap(e){
			let that = this;
			const action={
				back(){
					that.$Helper.tpfJump({type:"navigateBack"});
				},
				message(){
					that.$Helper.tpfJump({url:"/pages/ucenter/message-lists/message-lists"});
				}
			}
			if(action[e]) action[e]();
		},
		// 结算
		settlement(){
			//console.log("结算....",this.defaultAddress,this.defaultAddress.id);
			if(this.isEmptyAddress){
				this.$Helper.tpfToast({title:"请选择收货地址"});
				return false;
			}
			// console.log(this.cartGoodsList);
			
			/* 这里区分是直接购买，还是购物车结算 */
			// 如果是购买车结算
			
			uni.navigateTo({
				url:"/pages/shop/pay-order/pay-order"
			})
			
		}
	},
	computed:{
		productTotalCount() {
			let totalCount = 0;
			this.cartGoodsList.forEach(seller => {
				totalCount += seller.goods.length;
			});
			return totalCount;
		},
		totalMoney() {
			let totalMoney = new BigNumber(0);
			this.cartGoodsList.forEach(seller => {
				seller.goods.forEach(goods => {
					totalMoney = totalMoney.plus((new BigNumber(goods.goods_price)).multipliedBy(goods.number).toNumber());
				});
			});
			return Number(totalMoney);
		},
		isEmptyGoods(){
			return this.cartGoodsList.length==0;
		},
		isEmptyAddress(){
			return this.defaultAddress.length==0;
		}
	}
}